<?php

namespace App\Http\Controllers;

use App\Question;
use App\User;
use Illuminate\Http\Request;

class AdminController extends Controller
{
    //
    public function index()
    {
        return view('admin.home.index');
    }

    public function welcome()
    {
        return view('admin.home.welcome');
    }
    public function qlist(Request $request)
    {
        $where = [];
        if(!empty($request->start)){
            $where[]=['created_at','>',$request->start];
        }
        if(!empty($request->end)){
            $where[]=['created_at','<',$request->end];
        }
        if(!empty($request->title)){
            $where[]=['title','like',"%".$request->title."%"];
        }

        $questions = Question::where($where)->paginate(3);
        $page = isset($page)?$request['page']:1;
        $questions = $questions->appends(array(
            'title'=>$request['title'],
            'start'=>$request['start'],
            'end'=>$request['end'],
            'page'=>$page,
        ));
        return view('admin.question.list',compact('questions','request'));
    }
    public function jing(Question $question)
    {
        if ($question->is_jing ==1){
            $question->is_jing =0;
        }else{
            $question->is_jing =1;
        }
        $question->save();

    }

    public function top(Question $question)
    {
        if ($question->is_top ==1){
            $question->is_top =0;
        }else{
            $question->is_top =1;
        }
        $question->save();
    }
    public function questionDelete(Question $question)
    {
        $question->delete();
    }
    public function questionDeleteAll(Request $request)
    {
        Question::destroy($request->data);
    }

    public function ulist(Request $request)
    {
        $where = [];
        if(!empty($request->start)){
            $where[]=['created_at','>',$request->start];
        }
        if(!empty($request->end)){
            $where[]=['created_at','<',$request->end];
        }
        if(!empty($request->name)){
            $where[]=['name','like',"%".$request->name."%"];
        }

        $users = User::where($where)->paginate(1);
        $page = isset($page)?$request['page']:1;
        $users = $users->appends(array(
            'title'=>$request['title'],
            'start'=>$request['start'],
            'end'=>$request['end'],
            'page'=>$page,
        ));
        return view('admin.home.user',compact('users','request'));
    }

    public function userDelete(User $user)
    {
        if ($user->status ==1){
            $user->status =0;
        }else{
            $user->status =1;
        }
        $user->save();
    }
    public function userDeleteAll(Request $request)
    {
        User::whereIn('id',$request->data)
            ->update(['status'=>0]);
    }
}
